# -*- coding: utf-8 -*-
"""
Created on Sun Dec 15 17:07:36 2013

@author: daimoon
"""
import sys
sys.path.append("./modules")
import const
import numpy as np
from math import *

def lptau(i, j):
    frac = lambda x: x - trunc(x)
    in2 = lambda x: 2 ** x
    if i > 1020:
        i -= int(i / 1020) * 1020
        j += 2
    if i == 0:
        out = 0
    else:
        m = 1 + trunc(log2(i))
        out = 0
        for k in range(1, m + 1, 1):
            s = 0
            for l in range(m, k - 1, -1):
                s = s + trunc(2 * frac(i * in2(-l))) * trunc(2 * frac(const.lp_r[j][l] * in2(k - l - 1)))
            out = out + in2(1 - k) * frac(0.5 * s)
    return round(out, 8)

def m_lptau(s_i, s_j):
    m = np.zeros((s_i, s_j))
    for i in range(0, s_i):
        for j in range(0, s_j):
            m[i][j] = lptau(i+1, j+1)
    return m